package stage.action;

import java.util.*;

import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;

import stage.factory.AppFactory;
import stage.manager.*;
/**
 * the main action for getting the patient number. 
 * @author remi liu(Liu Zhibin)
 * @version 0.5
 */
public class QueryAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private Date date;
	private String hospital;
	private String department;
	private String isExpert;
	private String weather;
	private long[] num=new long[100];
	private String str; 
	
	public String query()throws Exception{
		double temp[];
		System.out.println(date);
		System.out.println(hospital);
		System.out.println(department);
		System.out.println(isExpert);
		int weekday=getWeekday(date);
		System.out.println(weekday);
		HistoricalInfosService hs=(HistoricalInfosService) AppFactory.instance().getApp("HistoricalInfosService");
		List l=hs.get(hospital, department,isExpert, weekday);
		List al=hs.getAll(hospital, department,isExpert);
		if(l==null||al==null)
			temp=new double[]{-1};
		else{
			temp=MainLogic.getVisitorNum(l,al,getIntWeather(weather));
			for(int i=0;i<temp.length&&i<8;i++){
				num[i]=Math.round(temp[i]);
			}
		}post(temp);
		return SUCCESS;
	}
	public int getWeekday(Date date){
		int day=date.getDay();
		if(day==0)day=7;
		return day;
	}
	public int getIntWeather(String weather){
		int w;
		if(weather.equals("晴")||weather.equals("多云"))
			w=1;
		else if(weather.equals("阴"))
			w=2;
		else if(weather.equals("小到中雨")||weather.equals("小到中雪")||weather.equals("雾"))
			w=3;
		else if(weather.equals("雷阵雨")||weather.equals("冰雹"))
			w=4;
		else if(weather.equals("大到暴雨")||weather.equals("大雪"))
			w=5;
		else w=0;
		return w;
	}
	/**
	 * post the visitor number to the web page by ajax.
	 * @param 	num		the visitor number
	 */
	public void post(double[] num){
		//TODO
	}
	public Date getServerDate(){
		return new Date();
	}
	/*
	 * getters and setters
	 */
	public Date getDate(){
		return this.date;
	}
	public void setDate(Date date){
		this.date=date;
	}
	public String getHospital(){
		return this.hospital;
	}
	public void setHospital(String hospital){
		this.hospital=hospital;
	}
	public String getDepartment(){
		return this.department;
	}
	public void setDepartment(String department){
		this.department=department;
	}
	public String getIsExpert(){
		return this.isExpert;
	}
	public void setIsExpert(String isExpert){
		this.isExpert=isExpert;
	}
	public String getWeather(){
		return this.weather;
	}
	public void setWeather(String weather){
		this.weather=weather;
	}
	@JSON(name="number")
	public long[] getNum(){
		return this.num;
	}
	public void setNum(long[] num){
		this.num=num;
	}
}
